package com.onix.scalecamerapreview.translate.msOcr;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.github.chrisbanes.photoview.PhotoView;
import com.onix.scalecamerapreview.R;
import java.io.File;
import java.io.InputStream;
import io.reactivex.functions.Consumer;


public class MsOcrActivity extends AppCompatActivity {
    String contents = "那时候" +
            "喜欢你\n" +
            "并不是因为你有车有房";
    private PhotoView msBitmapView;
    private PhotoView imageView;
    private byte[] content;
    private Bitmap bitmap;
    private InputStream inputStream;
    private String filePath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocr);
        msBitmapView = findViewById(R.id.ms_bitmap);
        findViewById(R.id.click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取assets资源管理器
                //通过管理器打开文件并读取
                MsRecognition.getInstance().execute(new CompressionRequest(getApplicationContext(),new File(filePath)
                        ,1024
                ),msBitmapView,"en");


            }
        });



        imageView = findViewById(R.id.newimage);
        findViewById(R.id.take).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAlbum();
            }
        });

    }


    public Bitmap viewConversionBitmap(View v) {
        int w = v.getWidth();
        int h = v.getHeight();

        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmp);

        c.drawColor(Color.WHITE);
        /** 如果不设置canvas画布为白色，则生成透明 */

        v.layout(0, 0, w, h);
        v.draw(c);

        return bmp;
    }
    private int REQUEST_CODE_CHOOSE = 87;
    private void openAlbum() {

        try {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE);
        }catch (Exception e){
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE) {
            if(data!=null){
                int defaultWidth = 1080;
                int defaultHeight = 1920;
                final Uri selectedUri = data.getData();
                filePath = MyFileUtils.getPath(this, selectedUri);
                MsRecognition.getInstance().compress(new CompressionRequest(getApplicationContext(),new File(filePath),1024))
                        .subscribe(new Consumer<File>() {
                            @Override
                            public void accept(File file) throws Exception {

                                byte[] contentByte =  MsRecognition.getInstance().File2Bytes(file);
                                Bitmap bitmap = BitmapFactory.decodeByteArray(contentByte, 0, contentByte.length);
                                imageView.setImageBitmap(bitmap);
                            }
                        });
//                Disposable subscribe = RxImageUtils.getBitmap(data.getData(), defaultWidth, defaultHeight)
//                        .subscribe(new Consumer<Bitmap>() {
//
//                            @Override
//                            public void accept(Bitmap bitmap) throws Exception {
//                                MsOcrActivity.this.bitmap = bitmap;
//                                imageView.setImageBitmap(bitmap);
//                            }
//                        });


            }

        }
    }
}
